package com.payment.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.ExcelUtil;
import com.payment.common.utils.StringUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.MoneyChange;
import com.payment.system.service.IMoneyChangeService;
import com.payment.system.service.ISysUserService;
import com.payment.system.vo.MoneyChangeNewOederVo;

/**
 * @author fuzhili
 * @Description:
 * @date 2019/3/16 8:56
 */
@Controller
@RequestMapping("/system/moneyChange")
public class MoneyChangeNewOrderController extends BaseController {
	
    private String prefix = "system/moneyChange";
    
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IMoneyChangeService moneyChangeService;


    /***
     * 跳转新订单分润返佣明细
     * @return
     */
    @RequiresPermissions("system:moneyChange:view")
    @GetMapping()
    public String moneyChangeNewOrder(ModelMap mmap)
    {
        Long userId=ShiroUtils.getUserId();
        String roleKey=userService.selectUserRoleKey(userId);
        mmap.put("role",roleKey);
        return prefix + "/moneyChangeNewOrder";
    }

    @RequiresPermissions("system:moneyChange:list")
    @PostMapping("/listNewOrder")
    @ResponseBody
    public TableDataInfo list(MoneyChangeNewOederVo moneyChangeVo)
    {
        Long userId= ShiroUtils.getUserId();
        String roleKey=userService.selectUserRoleKey(userId);
        if(StringUtils.equals(roleKey,"agent")){
            moneyChangeVo.setAgentId(ShiroUtils.getPhonenumber());
        }
        startPage();
        List<MoneyChangeNewOederVo> list= moneyChangeService.selectMoneyChangeVoListByNewOrder(moneyChangeVo);
        //统计分润金额和交易笔数
        Map<String,Object> map=moneyChangeService.selectCountMoneyChange(moneyChangeVo);
        if(list!=null&&list.size()>0){
            list.get(0).setNumber(Integer.parseInt(map.get("number").toString()));
            list.get(0).setTotalMoney(new BigDecimal((map.get("totalMoney").toString())));
        }
        return getDataTable(list);
    }

    /**
     * 导出资金变动列表
     */
    @RequiresPermissions("system:moneyChange:export")
    @Log(title = "导出资金变动列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MoneyChangeNewOederVo moneyChange)
    {
        Long userId= ShiroUtils.getUserId();
        String roleKey=userService.selectUserRoleKey(userId);
        if(StringUtils.equals(roleKey,"agent")){
            moneyChange.setAgentId(ShiroUtils.getPhonenumber());
        }
        List<MoneyChangeNewOederVo> list = moneyChangeService.selectMoneyChangeVoListByNewOrder(moneyChange);
        ExcelUtil<MoneyChangeNewOederVo> util = new ExcelUtil<MoneyChangeNewOederVo>(MoneyChangeNewOederVo.class);
        return util.exportExcel(list, "分润返佣明细");
    }




    /***
     * 跳转升级 分润界面
     * @return
     */
    @GetMapping("moneyChangeUpgrade")
    public String moneyChangeByUpgrade ()
    {
        return prefix + "/moneyChangeUpgrade";
    }

    @PostMapping("/listMoneyChangeByUpgrade")
    @ResponseBody
    public TableDataInfo listMoneyChangeByUpgrade(MoneyChangeNewOederVo upgradeVo)
    {
        startPage();
        List<MoneyChangeNewOederVo> list= moneyChangeService.selectMoneyChangeVoListByUpgrade(upgradeVo);
        Map<String,Object> map=moneyChangeService.selectCountMoneyChangeByUpgrade(upgradeVo);
        if(list!=null&&list.size()>0){
            list.get(0).setNumber(Integer.parseInt(map.get("number").toString()));
            list.get(0).setTotalMoney(new BigDecimal((map.get("totalMoney").toString())));
        }
        return getDataTable(list);
    }



    @PostMapping("/exportByUpgrade")
    @ResponseBody
    public AjaxResult exportByUpgrade(MoneyChangeNewOederVo upgradeVo)
    {
        Long userId= ShiroUtils.getUserId();
        String roleKey=userService.selectUserRoleKey(userId);
        if(StringUtils.equals(roleKey,"agent")){
            upgradeVo.setAgentId(ShiroUtils.getPhonenumber());
        }
        List<MoneyChangeNewOederVo> list = moneyChangeService.selectMoneyChangeVoListByUpgrade(upgradeVo);
        ExcelUtil<MoneyChangeNewOederVo> util = new ExcelUtil<MoneyChangeNewOederVo>(MoneyChangeNewOederVo.class);
        return util.exportExcel(list, "升级返佣明细");
    }


    @PostMapping("/shoppingScoreDetailedList")
    @ResponseBody
    public TableDataInfo shoppingScoreDetailedList(MoneyChange moneyChange)
    {
        startPage();
        List<MoneyChange> list= moneyChangeService.selectMoneyChangeList(moneyChange);
        return getDataTable(list);
    }


}
